﻿using RSFrame.Services;
using RSModels;
using System;
using System.Collections.Generic;
using System.Data.Common;

namespace AppointmentService
{
    class AppointmentService : IAppointmentService
    {
        public AppointmentService(IDBService dBService)
        {
            DBService = dBService;
        }

        public IDBService DBService { get; set; }

        public int? AddAppointmnet(Appointment appointment)
        {
            try
            {
                DbConnection c = DBService.EstablishConnection();
                object Result = DBService.AddData(c, "exec add_appointment @lng=@_lng, @lat=@_lat, @userID=@_userID, @address=@_address", new Dictionary<string, object>
                {
                    { "@_lng", appointment.Lng },
                    { "@_lat", appointment.Lat },
                    { "@_userID", appointment.UserID },
                    { "@_address", appointment.Address },
                });
                DBService.CloseConnection(c);
                return Convert.ToInt32(Result);
            }
            catch
            {
                return null;
            }
        }

        public bool CompleteAppointment(int id)
        {
            try
            {
                DbConnection c = DBService.EstablishConnection();
                object Result = DBService.AddData(c, "exec complete_appointment @id=@_id", new Dictionary<string, object>
                {
                    { "@_id", id },
                });
                DBService.CloseConnection(c);
                return Convert.ToInt32(Result) == 1;
            }
            catch
            {
                return false;
            }
        }

        public bool DeleteAppointment(int id)
        {
            throw new NotImplementedException();
        }

        public bool EditAppointment(int id)
        {
            throw new NotImplementedException();
        }

        public Appointment GetAppointment(int id)
        {
            try
            {
                Appointment result = null;
                DbConnection c = DBService.EstablishConnection();
                DbDataReader a = DBService.ReadData(c, "exec get_appointment_by_id @id=@_id",
                    new Dictionary<string, object> { { "@_id", id } });
                while (a.Read())
                {
                    result = new Appointment()
                    {
                        ID = Convert.ToInt32(a[0]),
                        Address = (string)a[1],
                        Lng = (float)Convert.ToDouble(a[2]),
                        Lat = (float)Convert.ToDouble(a[3]),
                        UserID = Convert.ToInt32(a[4]),
                        Processed = (string)a[5],
                    };
                }
                DBService.CloseConnection(c);
                return result;
            }
            catch
            {
                return null;
            }
        }

        public List<Appointment> GetAppointmentsByUserID(int userID)
        {
            try
            {
                List<Appointment> result = new();
                DbConnection c = DBService.EstablishConnection();
                DbDataReader a = DBService.ReadData(c, "exec get_msgs_by_appuserID @appuserID=@_appuserID",
                    new Dictionary<string, object> 
                    {
                        { "@_appuserID", userID}
                    });
                while (a.Read())
                {
                    result.Add(new Appointment()
                    {
                        ID = Convert.ToInt32(a[0]),
                        Address = (string)a[1],
                        Lng = (float)Convert.ToDouble(a[2]),
                        Lat = (float)Convert.ToDouble(a[3]),
                        UserID = Convert.ToInt32(a[4]),
                        Processed = (string)a[5],
                    });
                }
                DBService.CloseConnection(c);
                return result;
            }
            catch
            {
                return null;
            }
        }

        public List<Appointment> GetUnprocessedAppointments()
        {
            try
            {
                List<Appointment> result = new();
                DbConnection c = DBService.EstablishConnection();
                DbDataReader a = DBService.ReadData(c, "exec get_unprocessed_appointments",
                    new Dictionary<string, object> { });
                while (a.Read())
                {
                    result.Add(new Appointment()
                    {
                        ID = Convert.ToInt32(a[0]),
                        Address = (string)a[1],
                        Lng = (float)Convert.ToDouble(a[2]),
                        Lat = (float)Convert.ToDouble(a[3]),
                        UserID = Convert.ToInt32(a[4]),
                        Processed = (string)a[5],
                    });
                }
                DBService.CloseConnection(c);
                return result;
            }
            catch
            {
                return null;
            }
        }
    }
}
